package com.sg.hms.controller;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sg.hms.Dao.RoomDao;
import com.sg.hms.DaoImpl.RoomDaoImpl;
import com.sg.hms.Model.Getpath;
import com.sg.hms.Model.User;

/***
 * 添加器材数量
 * @author 吴磊
 *
 */
public class Addequiptmentnumcontroller extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	// TODO Auto-generated method stub
	doPost(req, resp);
}
@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int eId=Integer.parseInt(req.getParameter("eid"));
		int num=Integer.parseInt(req.getParameter("sum"));
		RoomDao room=new RoomDaoImpl();
		int count=room.updateEquiptmentNum(eId, num);
		User user=(User) req.getSession().getAttribute("currentuser");
		int id=user.getuId();
		String fpath=Getpath.getpath1();
		StringBuffer stringBuffer=new StringBuffer(fpath+"/AddqquiptmentNumber");
		stringBuffer.append(String.valueOf(id));
		Date date=new Date();
		SimpleDateFormat sim=new SimpleDateFormat("yyyyMMddHHmmss");
		String now=sim.format(date);
		stringBuffer.append(now);
		stringBuffer.append(".txt");
		String path=stringBuffer.toString();
		File file=new File(path);
		file.createNewFile();
		FileWriter writer=new FileWriter(file, true);
		String string="添加器材编号："+eId+"添加数量："+num;
		writer.write(string);
		writer.flush();
		writer.close();
		Writer writer2=resp.getWriter();
		writer2.write(String.valueOf(count));
		writer2.flush();
		writer2.close();
	}
}
